package com.xiaomaoguai.seria.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*
 * @Description :序号有14位数字组成，前8位为年月日，后六位为序号
 * @FileName: CreateSerialNum.java
 * @Author :WeiHui.Zhang
 * @Data : 2014年11月14日 下午12:23:40
 * @Version:1.0.0
 */
public class CreateSerialNum {

	private static Map<String, String> map = new HashMap<String, String>();
	private static String STATNUM = "000001";

	/**
	 * 获取年月日
	 * 
	 * @return
	 */
	public String getTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(cal.getTime());
	}

	/**
	 * 判断序号是否到了最后一个
	 * 
	 * @param s
	 * @return
	 */
	public String getLastSixNum(String s) {
		String rs = s;
		int i = Integer.parseInt(rs);
		i += 1;
		rs = "" + i;
		for (int j = rs.length(); j < 6; j++) {
			rs = "0" + rs;
		}
		return rs;
	}

	/**
	 * 产生不重复的号码 加锁
	 * 
	 * @return
	 */
	public synchronized String getNum() {
		String yearAMon = getTime();
		String last6Num = map.get(yearAMon);
		if (last6Num == null) {
			map.put(yearAMon, STATNUM);
		} else {
			map.put(yearAMon, getLastSixNum(last6Num));
		}

		return yearAMon + map.get(yearAMon);
	}

	public static void main(String[] args) {
		CreateSerialNum t = new CreateSerialNum();
		for (int i = 0; i < 200; i++) {
			System.out.println(t.getNum());
		}
	}
}
